import {
	asyncRoutes,
	constantRoutes
} from '/@/router'
import {
	toRaw
} from '@vue/reactivity'
import Layout from '/@/layout/index.vue'
import main from '/@/layout/main.vue'
import router from '../../router'
/**
 * 使用meta.role来确定当前用户是否有权
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
	if (route.meta && route.meta.roles) {
		return roles.some(role => route.meta.roles.includes(role))
	} else {
		return true
	}
}
let cache = []
async function paserRoute(m, component, id) {

	let compon = {
		layout: Layout,
		main: main
	};
	if (compon[component] != undefined) {
		return compon[component]
	}
	for (const path in m) {

		let nn = cache.find(i => i.name === component)

		if (nn != undefined) {
			return nn.res
		} else {
			const res = await m[path]()
			const mod = res.default
			const name = mod.name;
			cache.push({
				res: res,
				name: name
			})
			if (name != undefined && component == name) {
				return res;
			}
		}


	}


}




function parseRouterItem(mm, item, flag) {

	console.log(">>>item", item)

	let component = item.component
	let route
	if (item.children.length == 1) {
		let c = item.children[0]
		console.log('cccc', c)
		route = {
			path: item.path,
			name: item.name,
			component: Layout,
			redirect: item.redirect || '',
			children: [{
				path: c.path,
				name: c.name,
				component: () => paserRoute(mm, c.component, c.id),
				meta: {
					title: c.title,
					icon: c.icon,
					noCache: c.noCache ? true : false
				},
			}, ],
		}
	} else if (!flag || item.children.length != 0) {
		route = {
			path: item.path,
			name: item.name,
			params: item.params,
			component: () => paserRoute(mm, component, item.id),
			redirect: item.redirect || '',
			meta: {
				title: item.title,
				icon: item.icon,
				noCache: item.noCache ? true : false
			},
			children: parseRouterArrar(mm, item.children, false)
		}
	} else {

		route = {
			path: item.redirect,
			name: item.name,
			component: Layout,
			params: item.params,
			redirect: "/",
			meta: {
				title: "",
				noCache: false
			},
			children: [{
				path: item.path + str,
				name: item.name,
				component: () => paserRoute(mm, component, item.id),
				redirect: item.redirect || '',
				meta: {
					title: item.title,
					icon: item.icon,
					noCache: item.noCache ? true : false
				},
			}]
		}
	}


	return route;
}

function parseRouterArrar(mm, routes, flag) {
	let data = [];
	if (!routes) return data;

	routes.forEach(item => {
		let dd = parseRouterItem(mm, item, flag);
		data.push(dd)

	})
	return data;
}

/**
 * 通过递归过滤异步路由表
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
	const res = []
	routes.forEach(route => {
		const tmp = {
			...route
		}
		if (hasPermission(roles, tmp)) {
			if (tmp.children) {
				tmp.children = filterAsyncRoutes(tmp.children, roles)
			}
			res.push(tmp)
		}
	})
	return res
}

const state = {
	routes: [],
	addRoutes: []
}

const mutations = {
	SET_ROUTES: (state, routes) => {
		state.addRoutes = routes
		state.routes = constantRoutes.concat(routes)
		// console.log( 'accessRoutes1  state.routes', state.routes, toRaw(state.routes) )
	}
}

const actions = {
	generateRoutes({
		commit
	}, mrouter) {
		console.log('ro>>>', mrouter);
		return new Promise(resolve => {
			let m =
				import.meta.glob('../../views/**/*.vue')

			// let compon = {
			// 	layout: Layout,
			// 	main: main
			// };

			// for (const path in m) {

			// 	const res = await m[path]()
			// 	const mod = res.default
			// 	const name = mod.name;
			// 	if (name != undefined) {
			// 		compon[name] = res
			// 	}
			// 	console.log('mode>>>>>>>>>>>>>>>>>>>>>>>>', name);
			// }


			let accessedRoutes = parseRouterArrar(m, mrouter, true) || [];
			// if (roles.includes('admin')) {
			//   accessedRoutes = asyncRoutes || []
			// } else {
			//   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
			// }
			//const accessedRoutes = filterAsyncRoutes( asyncRoutes, roles )
			commit('SET_ROUTES', accessedRoutes)
			resolve(accessedRoutes)
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}